#include <iostream>
#include <algorithm>

using namespace std;
const int N = 100010;
int h[N], cnt;

void down(int x)
{
    int t = x;
    if (2 * x <= cnt && h[x] > h[2 * x])
    {
        t = 2 * x;
    }
    if (2 * x + 1 <= cnt && h[t] > h[2 * x + 1])
    {
        t = 2 * x + 1;
    }

    if (x != t)
    {
        swap(h[x], h[t]);
        down(t);
    }
}

int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
    {
        scanf("%d", &h[i]);
    }
    cnt = n;
    for (int i = n / 2; i; i--)
    {
        down(i);
    }

    while (m--)
    {
        printf("%d ", h[1]);
        h[1] = h[cnt--];
        down(1);
    }
    return 0;
}